﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfAppXx
{

    public partial class ComboSplitButton : ComboBox
    {
        private bool suspendUIEvents;

		public DataTemplate ButtonContentTemplate
		{
			get { return (DataTemplate)GetValue(ButtonContentTemplateProperty); }
			set { SetValue(ButtonContentTemplateProperty, value); }
		}

		// Using a DependencyProperty as the backing store for DefaultIndex.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ButtonContentTemplateProperty =
			DependencyProperty.Register("ButtonContentTemplate", typeof(DataTemplate), typeof(ComboSplitButton), new UIPropertyMetadata(null));


        public int DefaultIndex
        {
            get { return (int)GetValue(DefaultIndexProperty); }
            set { SetValue(DefaultIndexProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DefaultIndex.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DefaultIndexProperty =
            DependencyProperty.Register("DefaultIndex", typeof(int), typeof(ComboSplitButton), new UIPropertyMetadata(-1));

                
        public ICommand InsideClickCommand
        {
            get { return (ICommand)GetValue(InsideClickCommandProperty); }
            set { SetValue(InsideClickCommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for InsideClickCommand.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InsideClickCommandProperty =
            DependencyProperty.Register("InsideClickCommand", typeof(ICommand), typeof(ComboSplitButton), new UIPropertyMetadata(null));


        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Command.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(ComboSplitButton), new UIPropertyMetadata(null));


        static ComboSplitButton()
        {
        }

        public ComboSplitButton()
        {
            this.InitializeComponent();
            InsideClickCommand = new DelegateCommand(() => InsideClicked());
           
            //this will regisetre a method to execute when itemscontrol propoerty is changing
            //DependencyPropertyDescriptor.FromProperty(ItemsSourceProperty, typeof(ItemsControl)).AddValueChanged(this, OnMyPropertyChanged);
            DependencyPropertyDescriptor.FromProperty(DefaultIndexProperty, typeof(ComboSplitButton)).AddValueChanged(this, defaultIndexChanged);
        }


		private void defaultIndexChanged(object sender, EventArgs e)
		{
			suspendUIEvents = true;
			this.SelectedIndex = this.DefaultIndex;
			MainButtonVM mainButton = (this.DataContext as MainButtonVM);
            ButtonVM selectedButton = this.Items[this.DefaultIndex] as ButtonVM;
			mainButton.ToolTipX = selectedButton.ToolTipX;
			mainButton.Caption = selectedButton.Caption;
			mainButton.ButtonAction = selectedButton.ButtonAction;
			mainButton.ImageUri = selectedButton.ImageUri;
			suspendUIEvents = false;
		}

		protected override void OnSelectionChanged(SelectionChangedEventArgs e)
		{
			if (!suspendUIEvents)
			{
                ButtonVM selectedButton = (this.SelectedItem as ButtonVM);
				MainButtonVM mainButton = this.DataContext as MainButtonVM;
				mainButton.ToolTipX = selectedButton.ToolTipX;
				mainButton.Caption = selectedButton.Caption;
				mainButton.ButtonAction = selectedButton.ButtonAction;
				mainButton.ImageUri = selectedButton.ImageUri;
				base.OnSelectionChanged(e);
				InsideClicked();
			}
		}

        private void InsideClicked()
        {
            if (Command != null)
            {
                Command.Execute(null);
            }
        }
    }
}
